package router

import (
	"admin-system-go/model"
	"admin-system-go/model/dto"
	"admin-system-go/service"
	"github.com/gin-gonic/gin"
	"net/http"
)

type MenuRouter struct {
	menuService service.MenuService
}

func NewMenuRouter() MenuRouter {
	return MenuRouter{menuService: service.GetMenuService()}
}

func (MenuRouter MenuRouter) Register(r *gin.Engine) {
	r.GET(apiVersion+"/menu/list", MenuRouter.QueryMenu())
	r.POST(apiVersion+"/menu/create", MenuRouter.createMenu())
	r.POST(apiVersion+"/menu/delete", MenuRouter.deleteMenu())
	r.POST(apiVersion+"/menu/update", MenuRouter.updateMenu())
}

func (MenuRouter MenuRouter) QueryMenu() gin.HandlerFunc {
	return func(context *gin.Context) {
		var menuQueryDTO dto.MenuQueryDTO
		err := context.ShouldBind(&menuQueryDTO)
		if err != nil {
			context.JSON(http.StatusOK, model.ParamErrorModel)
			return
		}
		context.JSON(http.StatusOK, MenuRouter.menuService.QueryMenu(menuQueryDTO))
	}
}

func (MenuRouter MenuRouter) createMenu() gin.HandlerFunc {
	return func(context *gin.Context) {
		var menuBaseDTO dto.MenuBaseDTO
		err := context.ShouldBind(&menuBaseDTO)
		if err != nil {
			context.JSON(http.StatusOK, model.ParamErrorModel)
			return
		}
		context.JSON(http.StatusOK, MenuRouter.menuService.CreateMenu(menuBaseDTO))
	}
}

func (MenuRouter MenuRouter) deleteMenu() gin.HandlerFunc {
	return func(context *gin.Context) {
		var menuDelDTO dto.MenuDelDTO
		err := context.ShouldBind(&menuDelDTO)
		if err != nil {
			context.JSON(http.StatusOK, model.ParamErrorModel)
			return
		}
		context.JSON(http.StatusOK, MenuRouter.menuService.DelMenu(menuDelDTO))
	}
}

func (MenuRouter MenuRouter) updateMenu() gin.HandlerFunc {
	return func(context *gin.Context) {
		var menuUpdateDTO dto.MenuUpdateDTO
		err := context.ShouldBind(&menuUpdateDTO)
		if err != nil {
			context.JSON(http.StatusOK, model.ParamErrorModel)
			return
		}
		context.JSON(http.StatusOK, MenuRouter.menuService.UpdateMenu(menuUpdateDTO))
	}
}
